package cn.edu.ctbu.gudie.restcontroller;

import cn.edu.ctbu.gudie.domain.Patient;
import cn.edu.ctbu.gudie.service.PatientService;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/webapi/register")
public class RegisterRestController {
    @Autowired
    PatientService patientService;
    /**
     * 患者注册
     *
     * @param patientRegisterRequest
     * @return
     */
    @PostMapping("/patient")
    public int registerStudent(@RequestBody PatientRegisterRequest patientRegisterRequest) {
        // 构造患者对象
        Patient patient = new Patient();
        patient.setPatientNo(patientRegisterRequest.getPatientNo()); //
        patient.setPatientName(patientRegisterRequest.getPatientName());
        patient.setPatientPassword(patientRegisterRequest.getPatientPassword());
        patient.setPatientPhone(patientRegisterRequest.getPatientPhone());
        patient.setPatientSex(patientRegisterRequest.getPatientSex()); // 性别
        patient.setPatientAge(patientRegisterRequest.getPatientAge()); // 年龄

        // 保存对象到数据库
        try {
            patientService.save(patient);
            return 1; // 注册成功
        } catch (Exception e) {
            e.printStackTrace();
            return -1; // 注册失败
        }
    }

    public static class PatientRegisterRequest {
        // getters and setters
        @Setter
        @Getter
        private String patientName;
        @Setter
        @Getter
        private String patientPassword;
        @Setter
        @Getter
        private String patientNo; // 账号
        @Setter
        @Getter
        private String patientPhone;
        @Setter
        @Getter
        private Integer patientSex; // 性别
        @Setter
        @Getter
        private Integer patientAge; // 年龄
    }
}
